<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Gass_recruit_model extends CI_Model {
	
	public function &__get($key)
	{
		$CI =& get_instance();
		return $CI->$key;
	}
	
 	/**
	 * 获取所有的招生活动信息
	 *
	 * 根据结束时间来排序，越晚结束的排在最前面
	 */	
	function get_recruit_activities_list()
	{
		$uri = $this->uri->uri_to_assoc(3);     
		$limit = $this->config->item('pag_limit');
		$offset = (isset($uri['page'])) ? $uri['page'] : FALSE;	
		
		if (array_key_exists('search_key_words', $uri))
		{
			$pagination_url = 'gass_recruit/manage_recruit/search_key_words/'.$uri['search_key_words'].'/';
			$config['uri_segment'] = 6; // Changing to 6 will select the 6th segment, example 'controller/function/search/query/page/10'.
	    $search_key_words = str_replace('-',' ',urldecode($uri['search_key_words']));
	    
      $this->db->select('*');
      $this->db->from('ga_recruit_activities');
      $this->db->like('activity_alias', $search_key_words); 
      $this->db->limit($limit, $offset);	
      $this->db->order_by('activity_over','desc');    
	    $this->data['recruitListInfo'] = $this->db->get()->result_array();
      $this->db->select('*');
      $this->db->from('ga_recruit_activities');
      $this->db->like('activity_alias', $search_key_words); 
			$total_recruit = $this->db->get()->num_rows();				
	  }
	  else
	  {
	  	$search_key_words	='';
			$pagination_url = 'gass_recruit/manage_recruit/';
			$search_query = FALSE;
			$config['uri_segment'] = 4; 

      $this->db->select('*');
      $this->db->from('ga_recruit_activities');			
      $this->db->limit($limit, $offset);
      $this->db->order_by('activity_over','desc'); 	
	    $this->data['recruitListInfo'] = $this->db->get()->result_array();			
		  $total_recruit = $this->db->get('ga_recruit_activities')->num_rows();	
		}
		$this->load->library('pagination');	
		$config['base_url'] = base_url().$pagination_url.'page/';
		$config['total_rows'] = $total_recruit;
		$config['per_page'] = $limit; 
		$this->pagination->initialize($config); 
		$this->data['search_key_words'] = $search_key_words; // Populates search input field in view.
		$this->data['pagination']['links'] = $this->pagination->create_links();
		$this->data['pagination']['total_recruit'] = $total_recruit;
	}
	
 	/**
	 * 新增宣传活动
	 *
	 * 需要提前对宣传码进行处理
	 * 新增宣传活动并生成相应的宣传码
	 *
	 * 有几个问题还需要明确：1、招生宣传会是研招办来制定，还是各学院自己制定
	 * 2、需不需要指定哪个学院的宣传会？如果需要，就可以在招生码中有所显示
	 * 3、简单实现，不考虑招生码，直接采用自增的数字
	 * 4、宣传会是各学院去，还是研招办小组去？
	 *
	 */		
	function add_new_recruit()
	{
		$time_begin = $_POST['add_activity_begin'];
		$time_over = $_POST['add_activity_over'];
		
	 	$this->load->library('form_validation');
		$this->form_validation->set_rules('activity_alias', '招生宣传名称', 'required|max_length[50]');
		$this->form_validation->set_rules('add_activity_begin', '开始时间', 'required');
		$this->form_validation->set_rules('add_activity_over', '结束时间', 'required');
		if ($this->form_validation->run())
		{
	     if ($time_begin > $time_over)
	     {
	     	$this->data['message'] = "开始时间不能大于结束时间，请重新输入！";
	     	return FALSE;
	     }
		 	 $data = array(
               'activity_alias' => $_POST['activity_alias'],
               'activity_begin' => $time_begin,
               'activity_over' => $time_over
            );
       $this->db->insert('ga_recruit_activities', $data); 
		 	 redirect("gass_recruit/manage_recruit");
		}
	  else
	  {	
		  $this->data['message'] = validation_errors('<p class="error_msg">', '</p>');
			return FALSE;
		}
	}
 	/**
	 * 删除宣传活动
	 *
	 *
	 */		
	function update_recruit_activities()
	{
		if ($delete_activities = $this->input->post('delete_activity'))
		{
			foreach($delete_activities as $activity_id => $delete)
			{
				$this->db->delete('ga_recruit_activities', array('activity_id' => $activity_id));
			}
		}
		$this->session->set_flashdata('message', "成功删除招生活动!");
		redirect('gass_recruit/manage_recruit');	
	}
	
}
